package com.example.queryexpressdelivery_01.common;

import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.concurrent.CompletableFuture;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class HttpGetData {
    String host = "http://wuliu.market.alicloudapi.com";// 【1】请求地址 支持http 和 https 及 WEBSOCKET
    String appcode = "7dda8915f44d4ca389b32ed5b08580f3"; // 【3】开通服务后 买家中心-查看AppCode
    String path1 = "/kdi";
    String path2 = "/exCompany";
    String path3 = "/getExpressList";
    String path4 = "/unknown";
    String no;
    String type;
    String phone;
    OkHttpClient okHttpClient = new OkHttpClient();
    public HttpGetData(String no,String type,String phone){
        this.no = no;
        this.type = type;
        this.phone = phone;
    }

    public String QueryLogistics(){         // no参数需要有单号:电话尾号
        String urlSend = host + path1 + "?no=" + no;
        if (type!=null)urlSend+="&type="+type;
        try{
            return Send(urlSend).get();
        }catch (Exception e){
            Log.i("TAG","发送错误");
        }
        return null;
    }

    public String QueryExpressNumberCompany() {         // no参数需要有单号
        String urlSend = host + path2 + "?no=" + no;
        try{
            return Send(urlSend).get();
        }catch (Exception e){
            Log.i("TAG","发送错误");
        }
        return null;
    }

    public String QueryCompany(){         // 这里我们直接返回all
        String urlSend = host + path3 + "?type="+type;
        try{
            return Send(urlSend).get();
        }catch (Exception e){
            Log.i("TAG","发送错误");
        }
        return null;
    }

    public String Unknown(){            // 发送无法识别单号请求
        String urlSend = host + path4 + "?no="+no;
        if (phone!=null)urlSend+="&phone="+phone;
        try{
            return Send(urlSend).get();
        }catch (Exception e){
            Log.i("TAG","发送错误");
        }
        return null;
    }


    public CompletableFuture<String> Send(String urlSend){
        Request request = new Request.Builder()
                .url(urlSend)
                .addHeader("Authorization", "APPCODE "+appcode)
                .build();


        CompletableFuture<String> future = new CompletableFuture<>();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                future.completeExceptionally(e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    String responseBody = response.body().string();
                    future.complete(responseBody);
                } else {
                    future.completeExceptionally(new IOException("Unexpected code " + response));
                }
            }
        });
        future.thenAccept(response -> {
            // 成功获取响应
            System.out.println("Response: " + response);
        }).exceptionally(ex -> {
            // 处理异常
            ex.printStackTrace();
            return null;
        });
        return future;
    }
    public String read(InputStream is) throws IOException {
        StringBuffer sb = new StringBuffer();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = null;
        while ((line = br.readLine()) != null) {
            line = new String(line.getBytes(), "utf-8");
            sb.append(line);
        }
        br.close();
        return sb.toString();
    }

}
